package com.scitc.pleasurevideo.woview;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.data.UserDataMan;
import com.scitc.pleasurevideo.okgoutil.HttpCompleteOrFail;
import com.scitc.pleasurevideo.okgoutil.OkGoUtil;
import com.scitc.pleasurevideo.pojo.UserVo;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.util.ReflectionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginActivity extends ETActivity{

    private EditText mobileText;
    private EditText passwdText;
    private TextView tvSureBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        onclikc();
    }

    private void onclikc() {
        tvSureBtn.setOnClickListener(view -> tologin());

    }

    private void tologin(){
        String mobile = mobileText.getText().toString().trim();
        String passwd = passwdText.getText().toString().trim();
        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(passwd)) {
            Toast.makeText(mInstance, "用户名和 密码不能为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        OkGoUtil.init().toLogin(mobile, passwd, new HttpCompleteOrFail() {
            @Override
            public void onComplete(String json) {
                Map result = ETMan.getMananger().getGson().fromJson(json, HashMap.class);
                List<UserVo> userVos = ReflectionUtil.fillMapByReflect(UserVo.class, result);
                if(userVos==null||userVos.size()<1){
                    UserDataMan.init().saveUser("");
                    Toast.makeText(mInstance, "登录失败", Toast.LENGTH_SHORT).show();
                }else {
                    json =ETMan.getMananger().getGson().toJson(userVos.get(0));
                    UserDataMan.init().saveUser(json);
                    Toast.makeText(mInstance, "登录成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFile(String information) {
                Log.e("LoginActivity", information);
                UserDataMan.init().saveUser("");
                Toast.makeText(mInstance, "登录失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        mobileText = (EditText) findViewById(R.id.mobileText);
        passwdText = (EditText) findViewById(R.id.passwdText);
        tvSureBtn = (TextView) findViewById(R.id.tvSureBtn);
    }
}
